package com.easyexcle.demo.easyExcel.util;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author: lkz
 * @Title: WeekdayListGenerator
 * @Description: 计算某个时间段内的周 周一到周五
 * @Date: 2023/5/20 13:57
 */

public class WeekdayListGenerator {

    public static List<LocalDate> generateWeekdays(LocalDate start, LocalDate end) {
        List<LocalDate> weekdays = new ArrayList<>();

        // 获取开始时间和结束时间之间相差的天数
        long diffInDays = ChronoUnit.DAYS.between(start, end);

        // 遍历每一天，判断是否为周一到周五，如果是则加入列表
        for (int i = 0; i <= diffInDays; i++) {
            LocalDate date = start.plusDays(i);
            DayOfWeek dayOfWeek = date.getDayOfWeek();
            if (dayOfWeek != DayOfWeek.SATURDAY && dayOfWeek != DayOfWeek.SUNDAY) {
                weekdays.add(date);
            }
        }

        return weekdays;
    }

    public static void main(String[] args) {

        // 定义开始时间和结束时间
        LocalDate start = LocalDate.of(2023, 4, 26);
        LocalDate end = LocalDate.of(2023, 5, 31);

        // 生成时间段内的周一到周五的日期，并输出结果
        List<LocalDate> weekdays = generateWeekdays(start, end);
        System.out.println("时间段内的周一到周五的日期：");
        for (LocalDate date : weekdays) {
            System.out.println(date.toString());
        }

    }



}
